package org.springblade.tms.module.car.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.tms.global.entity.TimeInterval;
import org.springblade.tms.global.enums.ApprovalStatusEnum;
import org.springblade.tms.module.car.base.CarManagementService;
import org.springblade.tms.module.car.dto.CarDto;
import org.springblade.tms.module.car.importer.CarImporter;
import org.springblade.tms.module.car.vo.CarExcelVO;
import org.springblade.tms.module.car.vo.TMCarVO;
import org.springblade.tms.module.carriercarrelation.entity.TMCarrierCarRelation;
import org.springblade.tms.module.carriercarrelation.service.ICarrierCarRelationService;
import org.springblade.tms.module.common.dto.DeleteDto;
import org.springblade.tms.module.sap.dto.Carrier;
import org.springblade.tms.module.userinfo.base.DriverManagementService;
import org.springblade.tms.module.userinfo.dto.CarrierAuditReq;
import org.springblade.tms.module.userinfo.entity.TMUserInfo;
import org.springblade.tms.module.userinfo.service.IUserInfoService;
import org.springblade.tms.module.userinfo.vo.QualificationReviewCarrier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.module.car.controller
 * @className: CarManagementController
 * @author: yr
 * @description: 车辆管理
 * @date: 2022/3/9 11:40
 */
@RestController
@RequestMapping("car/CarManagementController")
@Api(value = "车辆管理-yr", tags = "车辆管理-yr")
public class CarManagementController {
	@Resource
	private CarManagementService carManagementService;
	@Resource
	private ThreadPoolExecutor dynamicTp;
	@Resource
	private Validator validator;
	@Resource
	private RedisTemplate redisTemplate;
	@Resource
	private DriverManagementService driverManagementService;
	@Resource
	private ICarrierCarRelationService carrierCarRelationService;
	@Resource
	private IUserInfoService userInfoService;

	/**
	 * @param car:
	 * @param query:
	 * @return R<IPage>
	 * @author yr
	 * @description 查询车辆列表
	 * @date 2022/3/9 11:48
	 */
	@ApiLog("查询车辆列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "查询车辆列表", notes = "传入car")
	public R<IPage> list(TMCarVO car, Query query, TimeInterval timeInterval) {
		return carManagementService.list(car, query, timeInterval);
	}

	/**
	 * @param car:
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 新增或修改车辆
	 * @date 2022/3/8 13:44
	 */
	@ApiLog("新增或修改车辆")
	@PostMapping("/saveOrUpdate")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "新增或修改车辆", notes = "传入userInfo")
	public R saveOrUpdate(@Valid @RequestBody CarDto car) {
		return carManagementService.saveOrUpdate(car, driverManagementService.getTmUserInfo());
	}


	/**
	 * @param car:
	 * @param query:
	 * @return R<IPage>
	 * @author yr
	 * @description 车辆资质审查列表
	 * @date 2022/3/9 11:48
	 */
	@ApiLog("车辆资质审查列表")
	@GetMapping("/qualificationReview")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "车辆资质审查列表", notes = "传入car")
	public R<IPage> qualificationReview(TMCarVO car, Query query, TimeInterval timeInterval) {
		car.setApprovalStatus(ApprovalStatusEnum.PENDING_REVIEW.getValue());
		return carManagementService.list(car, query, timeInterval);
	}

	/**
	 * @return
	 * @Desc 车辆审核
	 * @author yr
	 * @Date 2022/3/7
	 */
	@ApiLog("车辆审核")
	@PostMapping("/check")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "车辆审核", notes = "传入carrierAuditReq")
	public R check(@RequestBody List<CarrierAuditReq> carrierAuditReq) {
		return carManagementService.check(carrierAuditReq);
	}

	/**
	 * @param deleteDto:
	 * @return R<IPage < TMUserInfoVO>>
	 * @author yr
	 * @description 删除车辆
	 * @date 2022/3/14 15:44
	 */
	@ApiLog("删除车辆")
	@PostMapping("/delete")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "删除车辆", notes = "传入ids")
	public R delete(@Valid @RequestBody DeleteDto deleteDto) {
		return carManagementService.delete(deleteDto.getIds());
	}

	@ApiLog("导入Excel")
	@PostMapping("/importIntoExcel")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "导入Excel", notes = "传入file")
	public R importIntoExcel(@Valid MultipartFile file) {
		String importNo = UUID.randomUUID().toString();
		ExcelUtil.save(file, new CarImporter(carManagementService, dynamicTp, validator, driverManagementService.getTmUserInfo(), importNo, redisTemplate), CarExcelVO.class);
		Object num = redisTemplate.opsForValue().get(importNo);
		redisTemplate.delete(importNo);
		return R.data(num);
	}

	@ApiLog("车辆数据模板")
	@GetMapping("/export")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "车辆数据模板")
	public void exportNotice(HttpServletResponse response) {
		List<CarExcelVO> list = new ArrayList<>();
		ExcelUtil.export(response, "车辆数据模板", "车辆数据模板", list, CarExcelVO.class);
	}

	/**
	 * @param id:
	 * @return R<IPage>
	 * @author yr
	 * @description 车辆资质审查列表
	 * @date 2022/3/9 11:48
	 */
	@ApiLog("车辆资质审查承运商列表")
	@GetMapping("/qualificationReviewCarrierList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "车辆资质审查承运商列表", notes = "传入id")
	public R<List<QualificationReviewCarrier>> qualificationReviewCarrierList(Long id) {
		List<TMCarrierCarRelation> tmCarrierCarRelations = carrierCarRelationService.lambdaQuery().eq(TMCarrierCarRelation::getRelationCarNo, id).list();
		List<Long> carrierIds = tmCarrierCarRelations.stream().map(TMCarrierCarRelation::getRelationCarrierNo).collect(Collectors.toList());
		List<TMUserInfo> tmUserInfos = userInfoService.lambdaQuery()
			.in(!carrierIds.isEmpty(),TMUserInfo::getId, carrierIds)
			.apply(carrierIds.isEmpty(),"1=2")
			.list();
		return R.data(BeanUtil.copy(tmUserInfos, QualificationReviewCarrier.class));
	}
}
